package com.joycessh.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;

import com.joycessh.PO.cdgl.VenuePO;
import com.joycessh.VO.cdgl.VenueVO;
import com.joycessh.dao.ICdglDao;
import com.joycessh.dao.impl.CdglDaoImpl;
import com.joycessh.service.ICdglService;

public class CdglServiceImpl implements ICdglService {
	
	private VenuePO venuepo;
	private ICdglDao cdglDao;
	
	public CdglServiceImpl() {
		cdglDao = new CdglDaoImpl();
	}

	@Override
	public String addCourt(VenueVO venuevo) {
		// TODO Auto-generated method stub
		String msg = null;
		venuepo = new VenuePO();
		venuepo.setVenueId(venuevo.getVenueId());
		venuepo.setVenueName(venuevo.getVenueName());
		venuepo.setSport(venuevo.getSport());
		venuepo.setOpenTime(venuevo.getOpenTime());
		venuepo.setCloseTime(venuevo.getCloseTime());
		venuepo.setCourtNum(venuevo.getCourtNum());
		
		try {
			if(cdglDao.isExistId(venuevo.getVenueId())) {
				msg = "场馆编号已存在！";
			}else if(cdglDao.isExistName(venuevo.getVenueName())) {
				msg = "场馆名称已存在！";
			}else if(cdglDao.save(venuepo)) {
				msg = "success";
			}else {
				msg = "error";
			}
		}catch(HibernateException e) {
			msg = "系统繁忙，请稍后重试！";
		}
		return msg;
	}

	@Override
	public String updateCourt(VenueVO venuevo) {
		// TODO Auto-generated method stub
		String msg = null;
		try {
			if(cdglDao.isExistName(venuevo.getVenueName())) {
				msg = "场馆名称已存在！";
			}else if(cdglDao.update(venuevo)) {
				msg = "success";
			}else {
				msg = "error";
			}
		}catch(HibernateException e) {
			msg = "系统繁忙，请稍后重试！";
		}
		return msg;
	}

	@Override
	public String deleteCourt(String venueId) {
		// TODO Auto-generated method stub
		String msg = null;
		try {
			if(cdglDao.delete(venueId)) {
				msg = "success";
			}else {
				msg= "error";
			}
			
		}catch(HibernateException e) {
			msg = "系统繁忙，请稍后重试！";
		}
		return msg;
	}

	@Override
	public List<VenuePO> queryCourt(String queryText) {
		// TODO Auto-generated method stub
		List<VenuePO> result = new ArrayList<VenuePO>();
		try {
			result = cdglDao.query(queryText);
		}catch(HibernateException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<VenuePO> showAll() {
		// TODO Auto-generated method stub
		List<VenuePO> result = new ArrayList<VenuePO>();
		try {
			result = cdglDao.showAll();
		}catch(HibernateException e) {
			e.printStackTrace();
		}
		return result;
	}

}
